package de.urw.app;

import java.util.ArrayList;
import java.util.HashMap;

public class XMLObjectLinker {
	HashMap<String, Class<?>> modules;
	public ArrayList<URWKitObject> objects;
	public XMLObjectLinker(HashMap<String,Class<?>> modules, ArrayList<URWKitObject> objects){
		this.modules = modules;
		this.objects = objects;
	}
	public boolean hasModule(String moduleName){
		return this.modules.containsKey(moduleName);
	}
	@SuppressWarnings("rawtypes")
	public int createInstance(String moduleName){
		Class c = hasModule(moduleName)  ? this.modules.get(moduleName) : this.modules.get("Dummy");
		try {
			URWKitObject urwko = (URWKitObject) c.newInstance();
			this.objects.add(urwko);
			return this.objects.size()-1;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return -1;
	}
	public void pushData(int id, String key, Object data){
		URWKitObject urwko = this.objects.get(id);
		urwko.set(key, data);
	}
	
	public URWKitObject getByFieldId(Integer id){
		URWKitObject res = null;
		for ( int i = 0; i < this.objects.size(); i++){
			res = this.objects.get(i);
			if((Integer)res.get("id") == id){
				return res;
			}
		}
		return res;
	}
	
	
}
